$(document)
		.ready(
				function() {
					var contextPath = document.location.pathname;
					var index = contextPath.substr(1).indexOf("/");
					contextPath = contextPath.substr(1, index);
					var basePath = "/" + contextPath;
					var totalCount = $("#totalCount").val();
					var pSize = $("#pageSize").val();
					var uploader;
					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();
					if (pageCount == null || pageCount == ""
							|| pageCount == "undefined") {
						pageCount = 1;

					}
					initUploader();

					$('form')
							.submit(
									function() {
										if (typeof jQuery.data(this,
												"disabledOnSubmit") == 'undefined') {
											jQuery.data(this,
													"disabledOnSubmit", {
														submited : true
													});
											$(
													'input[type=submit], input[type=button]',
													this).each(
													function() {
														$(this).attr(
																"disabled",
																"disabled");
													});
											return true;
										} else {
											return false;
										}
									});

					$("#optBtn")
							.delegate(
									'#saveBtn',
									'click',
									function() {

										$("#cashDetail").attr("method", "post");

										var cashId = $(
												"#cashDetail input[id='cashId']")
												.val();
										// alert(cashId);
										if (cashId == "" || cashId == null
												|| cashId == "undefined") {

											$("#cashDetail").attr("action",
													"save.do");
											// alert("save");
										} else {
											$("#cashDetail").attr("action",
													"update.do");
											// alert("update");
										}
										$("#cashDetail").submit();

									});
					
					
					
					
					/***
					 * 
					 */
					$("#optBtn")
					.delegate(
							'#confirmBtn',
							'click',
							function() {

								$("#cashDetail").attr("method", "post");

								var cashId = $(
										"#cashDetail input[id='cashId']")
										.val();
								
									$("#cashDetail").attr("action",
											"confirm.do");
									
								$("#cashDetail").submit();

							});
					$('#pagination2')
							.jqPaginator(
									{
//										totalPages : pageCount,
//										visiblePages : 5,
//										currentPage : 1,
										totalPages : pageCount,
										visiblePages : 5,
										currentPage : 1,


										prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
											// alert("pageIndex="+num);
											// alert("type="+type);
											$("#pageIndex").val(num);
											var typeId = $("#proTypeId").val();
											// alert("type----Id="+typeId);
											var queryUrl = "queryByPage.do";
											$
													.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex : num,
															pageSize : pSize,
															typeId : typeId
														},
														cache : false,
														success : function(data) {

															$(
																	"#products tr:not(:first)")
																	.remove();
															var results = data.pageResult.resultList;
															// alert("----------->results.length=
															// "+results.length)
															for (i = 0; i < results.length; i++) {
																var cash = results[i];

																var rowContent = "<tr>"
																rowContent = rowContent
																		+ "<td><input name='id' class='checkbox' type='checkbox' value='"
																rowContent = rowContent
																		+ cash.id
																rowContent = rowContent
																		+ "'/></td>"

																rowContent = rowContent
																		+ "<td>"
																rowContent = rowContent
																		+ cash.num
																rowContent = rowContent
																		+ "</td>"
																rowContent = rowContent
																		+ "<td>"
																rowContent = rowContent
																		+ cash.creator
																rowContent = rowContent
																		+ "</td>"
																rowContent = rowContent
																		+ "<td>"
																rowContent = rowContent
																		+ cash.createTime
																rowContent = rowContent
																		+ "</td>"
																rowContent = rowContent
																		+ "<td>"
																rowContent = rowContent
																		+ cash.editor
																rowContent = rowContent
																		+ "</td>"
																rowContent = rowContent
																		+ "<td>"
																rowContent = rowContent
																		+ cash.editTime
																rowContent = rowContent
																		+ "</td>"

																		+ "<td>"
																if (cash.status == 0) {
																	rowContent = rowContent
																			+ "<span>未校验</span>"
																}
																if (cash.status == 1) {
																	rowContent = rowContent
																			+ "<span style='color:red;'>已校验</span>";
																}
																if (cash.status == 2) {
																	rowContent = rowContent
																			+ "<span style='color:blue;'>已送出</span>";
																}

																rowContent = rowContent
																		+ "</td><td>"
																rowContent = rowContent
																		+ cash.cashCode;
																rowContent = rowContent
																		+ "</td>"
																rowContent = rowContent
																		+ "<td>"
																rowContent = rowContent
																		+ cash.description
																rowContent = rowContent
																		+ "</td>";
																if (cash.status == 0) {
																	rowContent = rowContent
																			+ "<td><button name='editBtn' objId='"
																	rowContent = rowContent
																			+ cash.id
																	rowContent = rowContent
																			+ "' class='btn btn-sm-block'>修改</button></td>"
																	rowContent = rowContent
																			+ "<td><button name='vailBtn' objId='"
																	rowContent = rowContent
																			+ cash.id
																	rowContent = rowContent
																			+ "'"
																	rowContent = rowContent
																			+ "class='btn btn-sm-block'>校验</button>";
																}
																if (cash.status == 1||cash.status == 2) {
																	rowContent = rowContent
																			+ "<td><button name='viewBtn' objId='"
																	rowContent = rowContent
																			+ cash.id
																	rowContent = rowContent
																			+ "' class='btn btn-sm-block'>查看</button></td>"
																	rowContent = rowContent
																			+ "<td><button name='vailBtn' objId='"
																	rowContent = rowContent
																			+ cash.id
																	rowContent = rowContent
																			+ "'"
																	rowContent = rowContent
																			+ "class='btn btn-sm-block' disabled='disabled'>校验</button>";
																}
																rowContent = rowContent
																		+ "</td></tr>";
																$("#products")
																		.append(rowContent);
															}

														},
														error : function(html) {
															return;
														}
													});
										}
									});

					
					
					$("#products").delegate("[name='vailBtn']",'click',function(){
						cashId = $(this).attr("objId");
						var editUrl = "show.do";
						$.ajax({

							url : editUrl,
							dataType : 'json',
							data : {
								id : cashId
							},
							cache : false,

							success : function(data) {

								loadCash(data,"vail");
							},
							error : function(html) {
								// alert(html);
								alert("Load Data failly");
								refresh();
							}
						});
					});
					
					
					
					
					
					/**
					 * 全选当前页记录
					 */
					$("#products").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');

						$("input[name='id']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});

					/**
					 * 修改按钮事件
					 */

					$("#products").delegate('[name="editBtn"]', 'click',
							function() {

								cashId = $(this).attr("objId");

								var editUrl = "show.do";
								$.ajax({

									url : editUrl,
									dataType : 'json',
									data : {
										id : cashId

									},
									cache : false,

									success : function(data) {

										loadCash(data,"edit");
									},
									error : function(html) {
										// alert(html);
										alert("Load Data failly");
										refresh();
									}
								});

							});

					// $("#products").delegate('[name="viewResource"]', 'click',
					// function() {
					// var proId = $(this).attr("objId");
					// alert("////////proId=" + proId);
					//
					// loadResource(proId, 0);
					// });

					$("#products").delegate(
							'[name="viewBtn"]',
							'click',
							function() {
								var cashId = $(this).attr("objId");
								var editUrl="show.do";
								if (cashId != null && cashId != ""
										&& cashId != "undefined") {
									$.ajax({

										url : editUrl,
										dataType : 'json',
										data : {
											id : cashId

										},
										cache : false,

										success : function(data) {

											loadCash(data,"view");
										},
										error : function(html) {
											// alert(html);
											alert("Load Data failly");
											refresh();
										}
									});

								}

							});

					$("#tools").delegate('#deleteBtn', 'click', function() {
						delChk = $('input[name="id"]:checked');

						if (delChk.length > 0) {
							if (confirm("Are you sure delete the record?")) {

								/* var delUrl = "delete.do?ids="; */
								var delUrl = "delete.do";
								var idArr = new Array();

								for (var i = 0; i < delChk.length; i++) {
									idArr.push(delChk.get(i).value);

								}

								$.ajax({
									url : delUrl,
									type : "POST",
									data : {
										ids : idArr.toString()
									},
									cache : false,

									success : function(html) {
										alert("Delete successfully");
										refresh();
									},
									error : function(html) {
										alert("Delete failly");
										refresh();
									}
								});
							} else {
								return;
							}
						} else {
							alert("Please select the record to delete");
							return;
						}
					});

					$("#typeId")
							.delegate(
									$(this),
									'change',
									function() {

										var typeId = $(this).val();

										queryUrl = encodeURI("queryPrefixCodeByType.do?typeId="
												+ typeId + "");
										if (typeId != "" && typeId > 0) {
											$.ajax({
												url : queryUrl,
												dataType : 'text',

												cache : false,

												success : function(data) {

													$("#proCode").val(data)
												},
												error : function(data) {
													alert("获取产品编号出错");
												}
											});
										} else {
											alert("请选择产品类型");
										}
									});
					
					

					$("#tools").delegate('#addCash', 'click', function() {

						var typeId = $("#proTypeId").val();
						// alert("******proId******"+typeId);
						var editUrl = "add.do";
						$.ajax({

							url : editUrl,
							dataType : 'json',
							data : {
								id : typeId
							},
							cache : false,

							success : function(data) {

								// alert("************"+data.typeId);
								$("#cashDialog input").each(function() {
									$(this).prop("value", null);
									$(this).prop("readonly", false);

								});
								$("#cashDialog textarea").each(function() {
									$(this).prop("value", null);

								});

								loadCash(data,"add");

							},
							error : function(html) {
								alert(html);
								alert("Load Data failly");
								refresh();
							}
						});

					});

					//
					// function loadResource(proId, type) {
					//			
					// $("#resourceDialog input[id='proId']").val(proId);
					// var idx = 0;
					// $("#baseTab").addClass("active");
					// $("#pictrues").addClass("active");
					// $("#itemsTab").removeClass("active");
					// $("#videos").removeClass("active");
					// $("#resourceDialog").modal('show');
					//						
					//
					// }
					
					$('#uploadBtn').delegate($(this),'click',function() {
						if(uploader!=null && uploader!="undefined"){
							uploader.start();
							return false;
						}
				
					});
					/**
					 * 
					 */
					function initUploader() {
						uploader = new plupload.Uploader(
								{
									runtimes : 'html5,flash,silverlight,html4',
									browse_button : 'selectBtn', // you can
									// pass in
									// id...
									container : document
											.getElementById('imgUrl'), // ...
									// or
									// DOM
									// Element
									// itself
									url : 'upload.do',
									flash_swf_url : '<%=request.getContextPath()%>/manage/js/plupload/Moxie.swf',
									silverlight_xap_url : '<%=request.getContextPath()%>/manage/js/plupload/Moxie.xap',

									filters : {
										max_file_size : '10mb',
										mime_types : [ {
											title : "Image files",
											extensions : "jpg,gif,png"
										}

										]
									},

									init : {
										PostInit : function() {
											document.getElementById('imgUrl').value = '';

										},

										FilesAdded : function(up, files) {
											plupload
													.each(
															files,
															function(file) {
																document
																		.getElementById('imgUrl').value = "";
																document
																		.getElementById('imgUrl').value = file.name
																// +plupload.formatSize(file.size);
																		//document
//																		.getElementById('uploadBtn').onclick = function() {
//																	uploader.start();
//																	return true;
															//	};
															});
										},

										UploadProgress : function(up, file) {

										},
										FileUploaded : function(up, file,
												result) {
											alert("文件上传成功" + result.response);
											$("#previewCash").attr("src",
													result.response);
											$("#imgUrl").val(result.response);
										},

										Error : function(up, err) {
											// document.getElementById('console').innerHTML
											// += "\nError #" + err.code + ": "
											// + err.message;
											alert("上传文件失败");
										}

									}
								});
						uploader.init();
					}

					
					/**
					 * 
					 */
					function loadCash(cash,operation) {

						$("#cashId").val(cash.id);
						$("#cashCode").val(cash.cashCode);
						$("#caNum").val(cash.num);
						$("#previewCash").attr("src", cash.imgUrl);
						$("#imgUrl").val(cash.imgUrl);
						$("#note").val(cash.note);
						$("#status").val(cash.status);
						$("#description").val(cash.description);
						
						if(operation=="add"){
							$("#cashCode").removeAttr("readonly");
							$("#caNum").removeAttr("disabled");
							$("#selectBtn").removeAttr("disabled");
							$("#uploadBtn").removeAttr("disabled");
							$("#previewCash").removeAttr("disabled");
							$("#imgUrl").removeAttr("readonly");
							$("#note").removeAttr("readonly");
							$("#description").removeAttr("readonly");
							$("#saveBtn").show();
							$("#confirmBtn").hide();
						}
						
						if(operation=="view"){
							$("#cashCode").attr("readonly","readonly");
							$("#caNum").attr("disabled","disabled");
							$("#selectBtn").attr("disabled","disabled");
							$("#uploadBtn").attr("disabled","disabled");
							$("#previewCash").attr("disabled","disabled");
							$("#imgUrl").attr("readonly","readonly");
							$("#note").attr("readonly","readonly");
							$("#description").attr("readonly","readonly");
							$("#saveBtn").hide();
							$("#confirmBtn").hide();
						}
						if(operation=="edit"){
							$("#cashCode").removeAttr("readonly");
							$("#caNum").removeAttr("disabled");
							$("#selectBtn").removeAttr("disabled");
							$("#uploadBtn").removeAttr("disabled");
							$("#previewCash").removeAttr("disabled");
							$("#imgUrl").removeAttr("readonly");
							$("#note").removeAttr("readonly");
							$("#description").removeAttr("readonly");
							$("#saveBtn").show();
							$("#confirmBtn").hide();
						}
						if(operation=="vail"){
							$("#cashCode").removeAttr("readonly");
							$("#caNum").removeAttr("disabled");
							$("#selectBtn").removeAttr("disabled");
							$("#uploadBtn").removeAttr("disabled");
							$("#previewCash").removeAttr("disabled");
							$("#imgUrl").removeAttr("readonly");
							$("#note").removeAttr("readonly");
							$("#description").removeAttr("readonly");
							$("#saveBtn").hide();
							$("#confirmBtn").show();
						}
						$("#cashDialog").modal("show");
					}

//$("#cashDialog").on('show.bs.modal', function() {
//	if (cash.status == 1) {
//		
//		$("#cashCode").attr("readonly","readonly");
//		$("#caNum").attr("disabled","disabled");
//		$("#previewCash").attr("disabled","disabled");
//		$("#imgUrl").attr("readonly","readonly");
//		$("#note").attr("readonly","readonly");
//		$("#description").attr("readonly","readonly");
//	}
//	if (cash.status == 0) {
//		$("#cashCode").removeAttr("readonly");
//		$("#caNum").removeAttr("disabled");
//		$("#previewCash").removeAttr("disabled");
//		$("#imgUrl").removeAttr("readonly");
//		$("#note").removeAttr("readonly");
//		$("#description").removeAttr("readonly");
//	}
//					});


					function vaildate(form) {
						return true;
					}

					function refresh() {
						window.location.reload();
					}
				});
